/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.client;

import avl.sv.client.tools.FreehandTool;
import avl.sv.client.tools.IntelligentScissorsTool;
import avl.sv.client.tools.OvalTool;
import avl.sv.client.tools.PanTool;
import avl.sv.client.tools.PointTool;
import avl.sv.client.tools.PolygonTool;
import avl.sv.client.tools.RectangleTool;
import avl.sv.client.tools.SelectTool;
import avl.sv.client.tools.AbstractSlideViewerTool;
import avl.sv.client.tools.PointPushTool;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.JToggleButton;

/**
 *
 * @author benbryan
 */
public class SlideViewerToolPanel extends javax.swing.JPanel {
    private final AbstractSlideViewerTool defaultTool;
    private final SlideViewer slideViewer;
    final int toolDim = 40;
    final int toolPad = 2;
    
    private final ArrayList<AbstractSlideViewerTool> tools = new ArrayList<>();
        
    public SlideViewerToolPanel(SlideViewer slideViewer) {
        initComponents();
        this.slideViewer = slideViewer;
        setSize(0,0);
        defaultTool = new PanTool(slideViewer);
        addTool(defaultTool);
        addSpace();
        addTool( new SelectTool(slideViewer) );
        addTool( new PointTool(slideViewer) );
        addTool( new PointPushTool(slideViewer));
        addSpace();
        addTool( new FreehandTool(slideViewer) );
        addTool( new RectangleTool(slideViewer) );
        addTool( new OvalTool(slideViewer) );
        addTool( new PolygonTool(slideViewer) );
        addTool( new IntelligentScissorsTool(slideViewer) );
        
        buttonPress(defaultTool);
    }
    
    private void addSpace(){
        JToggleButton space = new JToggleButton();
        add(space);
        space.setSize(toolDim/5, toolDim);
        space.setLocation(x, 0);
        space.setVisible(true);
        space.setEnabled(false);
        x += space.getSize().width + toolPad;
        setSize(x, toolDim);
    }
    
    private int x = toolPad;    
    final public void addTool(final AbstractSlideViewerTool tool){
        tools.add(tool);
        add(tool);
        tool.setSize(toolDim, toolDim);
        tool.setLocation(x, 0);
        tool.setVisible(true);
        x += toolDim + toolPad;
        tool.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttonPress(tool);
            }
        });
        setSize(x, toolDim);
    }
   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 44, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 34, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    public AbstractSlideViewerTool getSelectedTool(){
        for (AbstractSlideViewerTool tool:tools){
            if (tool.isSelected()){
                return tool;
            }
        }
        buttonPress(defaultTool);
        return defaultTool;
    }
            
    public void setDefaultTool(){
        buttonPress(defaultTool);
    }
    
    private void buttonPress(AbstractSlideViewerTool src){
        for (AbstractSlideViewerTool tool:tools){
            tool.setSelected(false);
            slideViewer.removeMouseListener(tool);
            slideViewer.removeMouseMotionListener(tool);
        }
        src.setSelected(true);
        slideViewer.addMouseListener(src);
        slideViewer.addMouseMotionListener(src);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    void paintTools(Graphics g) {
        for (AbstractSlideViewerTool tool:tools){
            Graphics2D g2d = (Graphics2D) g.create();
            tool.paintOnSlideViewer(g2d);
        }
    }
}
